import { ActivityVO, getActivityPage, removeActivity } from '@/services/ActivityService'
import { useAccess, useModel } from '@@/exports'
import { ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Popconfirm, Space } from 'antd'

export interface ActivityProps {
  tableRef?: any
}

const ActivityTable = (props: ActivityProps) => {
  const access = useAccess()

  let { getDictData, getDictEnum } = useModel('dict')

  /**
   * 列定义
   */
  const columns: ProColumns<ActivityVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      width: 120,
      hideInTable: true,
      hideInSearch: true,
    },
    {
      title: '活动名称',
      dataIndex: 'activityName',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '活动标题',
      dataIndex: 'activityTitle',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '粉丝量计算规则',
      dataIndex: 'countValueType',
      width: 120,
      valueEnum: getDictEnum('bh_value_count_rule'),
      hideInSearch: true,
    },
    {
      title: '分享标题',
      dataIndex: 'shareTitle',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '分享封面图',
      dataIndex: 'shareCover',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '活动开始时间',
      dataIndex: 'startTime',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '活动结束时间',
      dataIndex: 'endTime',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '操作',
      render: (_: any, record: ActivityVO) => [
        <Space key="operate">
          {access.hasActivityUpdatePerm && (
            <Button type="link" size="small" href={`/admin/mp/bh/edit?type=edit&activityId=${record.id}`}>
              编辑
            </Button>
          )}
          {access.hasActivityUpdatePerm && (
            <Popconfirm
              title="确定要删除吗"
              onConfirm={() => {
                removeActivity(record.id).then(() => props.tableRef?.current?.resetFields())
              }}
              okText="确认"
              cancelText="取消"
            >
              <Button type="link" danger size="small">
                删除
              </Button>
            </Popconfirm>
          )}
        </Space>,
      ],
      hideInSearch: true,
    },
  ]

  // 工具栏
  let actions = [
    access.hasActivitySavePerm && (
      <Button key="create" type="primary" href={`/admin/mp/bh/edit?type=new`}>
        添加
      </Button>
    ),
  ]

  return (
    <>
      <ProTable
        actionRef={props.tableRef}
        columns={columns}
        request={getActivityPage}
        toolbar={{
          actions: actions,
        }}
        scroll={{ x: 500 }}
        sticky={{ offsetHeader: 55, offsetScroll: 5 }}
      ></ProTable>
    </>
  )
}

export default ActivityTable
